<template>
	<view v-if="isShow">
		<Header title="会员卡支付" ></Header>
		<view @click="change(index)" :class="{'active':index==curIndex}" v-for="(item,index) in list" class="card">
			姓名：{{item.ownerName}}(余额：{{item.amount}})
		</view>
		<button v-if="list.length>0" @click="summit" type="default">余额支付</button>
		<view v-else class="quesheng">
			您目前还没有绑定卡哦
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {				
				list:[],
				curIndex:0,
				appId:getApp().globalData.appId,
				isShow:false
			}
		},
		onLoad(e) {
			if(e.mid){
				getApp().globalData.mid = e.mid
				getApp().globalData.num = e.d.split(',')[0]
			}
			if(e.isBind){
				this.getCardList()
			}else{
				this.login()
			}
		},
		methods: {
			async summit(){
				if(!this.list[this.curIndex].chipNo){
					await this.convertMemberCard()
				}
				this.$http('/member/api/memberCard/consumeByAmount',{
					"mId":getApp().globalData.mid,
					"slotNo":getApp().globalData.num,
					"chipNo":this.list[this.curIndex].chipNo
				},res=>{
					if(res.data.status==500){
						uni.showToast({
							icon:'none',
							title:res.data.errorMsg
						})						
					}else{
						uni.showToast({
							title:'购买成功'
						})
						setTimeout(()=>{
							uni.reLaunch({
								url:'/pages/index/index?type=2'
							})
						},2000)
					}
					
					
				})
			},
			login(){
				let that=this
				uni.login({
					success(res2) {
						that.getOpenId(res2.code)
					}
				})
			},
			getOpenId(code){
							this.$http('/pay/api/wechart/getSessionNew',{
											code:code,
											appId:this.appId
										},res3=>{
											uni.setStorageSync('openId',res3.data.openid)
											uni.setStorageSync('sk',res3.data.sk)
											this.getLogin()
										},'GET')
			},
			getLogin(){
				this.$http('/minprogram/api/member/verify',{
					"wxOpenId":uni.getStorageSync('openId'),
					"pubOpenId": uni.getStorageSync('pubopenid') || "",
				},res4=>{
						uni.setStorageSync('token',res4.data.token)
						this.getCardList()
				},'POST',()=>{
					uni.reLaunch({
						url:'/pages/exit/exit'
					})
				})
			},
			getCardList(){
				this.$http('/minprogram/api/member/getOwnerMemberCardList',{
					condition:{
							dataTypeFlag:2
						}
				},async res=>{
					this.list=res.data.items
					this.isShow=true
					for (let item of this.list) {
					  item.ownerName = await this.decryptPhone(item.ownerName); 
					}
				})
			},
			decryptPhone(value) {
				if (value === null || value === undefined || value.trim() === '') {
				  return Promise.resolve('');
				}
				return new Promise((resolve,reject)=>{
					// if (value.length > 20 && /[0-9]/.test(value) && /[a-zA-Z]/.test(value)) {
					// if (/[0-9]/.test(value) && /[a-zA-Z]/.test(value)) {
						this.$http('/kernel/api/privacy/decryptPhone',{"CipherTexts":[value] }, res=>{
							if(res.data.data.length>0){
									resolve(res.data.data[0].phone) 
								}else{
									resolve(value)
								}
						})
					// }else{
						// resolve(value)
					// }
				})
			},
			convertMemberCard(){
				return new Promise((suc,fail)=>{
					this.$http('/minprogram/api/member/convertMemberCard',{
						"id":this.list[this.curIndex].id,
						"property1":uni.getStorageSync('openId')
					},res=>{
						this.list[this.curIndex].chipNo = res.data.value
						suc()
					})
				})
				
			},
			change(index){
				this.curIndex=index
			}
		}
	}
</script>

<style scoped lang="less">
.card{
	width: 95%;
	margin: 30upx auto 0;
	background-color: #FFFFFF;
	border-radius: 12upx;
	height: 120upx;
	line-height: 120upx;
	font-size: 32upx;
	text-indent: 80upx;
}
.card.active{
	background-color: #4CD964;
	color: #FFFFFF;
}
button{
	background-color: #4CD964;
	width: 95%;
	height: 80upx;
	line-height: 80upx;
	color: #FFFFFF;
	font-size: 30upx;
	margin-top: 60upx;
}
.quesheng{
	text-align: center;
	font-size: 24upx;
	color: #999999;
	height: 300upx;
	line-height: 300upx;
}
</style>
